package com.telecom.restaurant.aop;

import com.telecom.restaurant.constant.ManageThreadLocal;
import com.telecom.restaurant.constant.UserThreadLocal;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.service.AdminService;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by qiqi on 2019/12/3.
 */
@Aspect
@Component
public class AuthAspect {

    @Autowired
    AdminService adminService;


    // 定义切点
    @Pointcut("execution(* com.telecom.restaurant.controllor.manage.*.*(..)) " +
            "&& !execution(* com.telecom.restaurant.controllor.manage.RestaurantController.adminLogin(..)) )")
    public void setAdminTokenPointCut() {
    }

    // 后置处理
    @AfterReturning(value = "setAdminTokenPointCut()", returning = "result")
    public void setAdminToken(DataResult result) {
        String token = adminService.generateAdminToken(ManageThreadLocal.userId.get(), ManageThreadLocal.restaurantId.get());
        result.setToken(token);
    }


    // 定义切点
    @Pointcut("execution(* com.telecom.restaurant.controllor.user.*.*(..)) " +
            "&& !execution(* com.telecom.restaurant.controllor.user.UserLoginController.callbackFromWX(..)) )")
    public void setuseTokenPointCut() {
    }

    // 后置处理
    @AfterReturning(value = "setuseTokenPointCut()", returning = "result")
    public void setuseToken(DataResult result) {
        String token = adminService.generateUseToken(UserThreadLocal.userId.get(), UserThreadLocal.mobile.get());
        result.setToken(token);
    }


}
